<%--
  Created by IntelliJ IDEA.
  User: Shuwei  Wang
  Date: 2020/6/18
  Time: 15:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body>

<button class="layui-btn" id="toAddAdmin">添加管理员</button>
<form class="layui-form" action="" id="addAdminForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="username" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submitadmin">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<%--修改上师--%>
<form class="layui-form" action="" lay-filter="update" id="updAdminForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="username" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input type="hidden" name="id"/>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updadmin">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test" ></table>
<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
<script>
    //Demo
    layui.use(['form','table','layer','upload'], function(){
        var form = layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var upload=layui.upload;
        var index;
        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '${pageContext.request.contextPath}/admin/allAdmin' //数据接口
            ,limit:3
            ,limits:[3,6]
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: '管理员编号',  sort: true/*, fixed: 'left'*/}
                ,{field: 'username', title: '名字'}


                ,{field: 'password', title: '密码'}


                ,{title: '操作',toolbar: '#barDemo'}

            ]]
        });

        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if(layEvent === 'detail'){ //查看
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"${pageContext.request.contextPath}/admin/delAdmin",
                        data:"id="+data.id,
                        dataType:"text",
                        type:"post",
                        success:function (d) {
                            if(d=="true"){
                                layer.msg("删除成功！");
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }else{
                                layer.msg("删除失败！");
                                layer.close(index);
                            }
                        }
                    });


                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                $("[name='id']").val(data.id);
                form.val('update',data)
                <%--$.ajax({--%>
                <%--url:"${pageContext.request.contextPath}/guru/selOneGuru",--%>
                <%--data:"id="+data.guruId,--%>
                <%--dataType:"json",--%>
                <%--type:"post",--%>
                <%--success:function (d) {--%>
                <%--$("[name='guruId']").val(d.guruId);--%>
                <%--$("[name='guruName']").val(d.guruName);--%>
                <%--$("[name='guruImage']").val(d.guruImage);--%>
                <%--$("#updguruImage").prop("src","${pageContext.request.contextPath}/img/"+d.guruImage);--%>
                <%--$("[name='guruNickname']").val(d.guruNickname);--%>

                <%--if(d.guruStatus==1){--%>

                <%--/*$("#shi").prop("checked",true);*/--%>

                <%--$(document).on("#shi",function () {--%>
                <%--$(this).prop("checked","checked");--%>
                <%--});--%>

                <%--}else {--%>
                <%--$("#fou").prop("checked",true);--%>
                <%--}--%>


                <%--}--%>
                <%--});--%>

                index=layer.open({
                    type:1,
                    area:['500px','500px'],
                    content:$("#updAdminForm"),
                    title:"修改管理信息"
                });
                form.on("submit(updadmin)",function (data) {
                    var data=data.field;
                    $.ajax({
                        url:'${pageContext.request.contextPath}/admin/updAdmin',
                        data:data,
                        dataType:'text',
                        type:"post",
                        success:function (data1) {
                            if(data1=='true'){
                                layer.msg('修改成功');
                                //同步更新缓存对应的
                                // $("#updGuruForm").get(0).reset();
                                table.reload("demo");

                                layer.close(index);
                            }else {
                                layer.msg('修改失败！');
                            }
                        }
                    })
                    return false;
                });


            } else if(layEvent === 'LAYTABLE_TIPS'){
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });





        $("#toAddAdmin").click(function () {
            index= layer.open({
                title:"添加管理员",
                area:['400px','400px'],
                content:$("#addAdminForm"),
                type:1
            })
        })
        //监听提交
        form.on('submit(submitadmin)', function(data){

           // layer.msg(JSON.stringify(data.field));
            var data=data.field;
            $.ajax({
                url:"${pageContext.request.contextPath}/admin/addAdmin"
                ,data:data
                ,dataType:"text"
                ,type:"post"
                ,success:function (data) {
                    if(data=="true"){
                        layer.msg("添加管理员成功！");
                        table.reload("demo");
                        layer.close(index);
                    }else{
                        layer.msg("添加失败！");
                    }
                }
            })
            return false;
        });
    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
    {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
    {{#  } }}
</script>

<style type="text/css">
    .layui-table-cell{
        text-align:center;
        height: auto;
        white-space: normal;
    }
</style>
</body>
</html>
